package homework3;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class CountHashMap {
    private String string;

    public CountHashMap(String string) {
        setString(string);
    }

    public String getString() {
        return string;
    }

    public void setString(String string) {
        this.string = string;
    }

    public void count() {
        //声明一个map集合
        Map<String, Integer> map = new HashMap<>();
        //切割字符串
        String[] str = string.split(",");
        System.out.println(Arrays.toString(str));
        //遍历数组，统计字符出现次数并放在map集合中
        for (String s : str) {
            Integer integer = map.get(s);
            if (integer != null) {
                integer++;
                map.put(s, integer);
            } else {
                map.put(s, 1);
            }
        }
        //打印统计内容
        map.forEach((key, var) -> {
            System.out.println(key + "：出现了 " + var + " 次！");
        });
    }
}
